<?php 
/**
 * 阿里软件平台的淘宝API操作类
 * 
 * 具体参数参照API文档
 * http://isp.alisoft.com/apidoc/api/apiIndex.html
 */
class TaobaoApiClass{

	/**
	 * 软件注册ID
	 *
	 * @var int
	 */
	var $APP_ID = 14237;
	/**
	 * 接口密钥
	 *
	 * @var string
	 */
	var $CERT_CODE = '560dcef0796b11ddb832e0acd9ea39ab';
	/**
	 * 接口地址
	 *
	 * @var string
	 */
	var $SIP_URL_PRE = 'http://sip.alisoft.com/sip/rest';
	/**
	 * 接口版本号
	 *
	 * @var string
	 */
	var $SIP_VER = '1.0';
	/**
	 * 时间戳记，API必备参数，格式'2008-05-02 21:18:58'
	 *
	 * @var date
	 */
	var $SIP_TIMESTAMP;
	/**
	 * sessionid由ISV网站生成，可以为空。这是一个加强ISV网站安全的参数。
	 *
	 * @var string
	 */
	var $SIP_SESSIONID;
	/**
	 * 模拟http操作对象
	 *
	 * @var object
	 */
	var $proxy;

	/**
	 * 构造方法
	 */
	function __construct()
	{
		$this->TaobaoApiClass();
	}
	function TaobaoApiClass(){
		require_once("Snoopy.class.php");
		$this->proxy = new Snoopy();

		$this->SIP_TIMESTAMP = date("Y-m-d H:i:s");
		$this->SIP_SESSIONID = '294fe843d98a455da8918f472a9c3c93';
	}

	/**
     * 免登验证
     *
     * @param int $userId 用户id
     * @param int $appId 软件注册ID
     * @param int $appInstanceId 远程应用ID
     * @param string $token 用户口令环
     */
	function validateUserByREST($userId, $appId, $appInstanceId, $token){

		$token = $token;

		$formVars['sip_timestamp'] = $this->SIP_TIMESTAMP;
		$formVars['sip_appkey'] = $this->APP_ID;
		$formVars['appId'] = $this->APP_ID;
		$formVars['token'] = $token;
		$formVars['appInstanceId'] = $appInstanceId;
		$formVars['userId'] = $userId;
		$formVars['sip_apiname'] = 'alisoft.validateUser';
		$formVars['sip_sessionid'] = $this->SIP_SESSIONID;

		ksort($formVars);
		$signStr = $this->CERT_CODE;
		foreach ($formVars as $key => $val) {
			$signStr.= $key;
			$signStr.= $val;
		}
		$sip_sign = strtoupper(md5($signStr));
		$formVars['sip_sign'] = $sip_sign;
		//向AEP发出服务端请求
		$this->proxy->submit($this->SIP_URL_PRE,$formVars);
		print_r($this->proxy);
	}
	/**
	 * 淘宝绑定引导登录页
	 *
	 * @param string $redirect_url 登录后返回地址
	 */
	function getUserLogin($redirect_url){
		header("location: http://sip.alisoft.com/sip/login?sip_apiname=taobao.items.onsale.get&sip_appkey=14237&ispid=14237&sip_sessionid=aRzmY4FFAv29AGTnrP&sip_redirecturl=".$redirect_url);
	}
	/**
	 * 取单个用户信息
	 *
	 * @param string $nick 用户名
	 * @param string $sip_format 返回信息格式，默认xml
	 * @return xml
	 */
	function getUserInfo($nick, $sip_format = 'xml'){
		$formVars['sip_timestamp'] = $this->SIP_TIMESTAMP;
		$formVars['sip_appkey'] = $this->APP_ID;
		$formVars['sip_apiname'] = "taobao.user.get";
		$formVars['v'] = $this->SIP_VER;
		$formVars['fields'] = "nick,sex,buyer_credit,seller_credit,location.city,location.state,location.country,created,last_visit,id_card,mobile,location.address";
		$formVars['nick'] = $nick;
		$formVars['sip_sessionid'] = $this->SIP_SESSIONID;
		$formVars['format'] = $sip_format;
		ksort($formVars);
		$signStr = $this->CERT_CODE;
		foreach ($formVars as $key => $val) {
			$signStr.= $key;
			$signStr.= $val;
		}
		$sip_sign = strtoupper(md5($signStr));
		$formVars['sip_sign'] = $sip_sign;

		//向AEP发出服务端请求
		$this->proxy->submit($this->SIP_URL_PRE,$formVars);
		return ($this->proxy->results);

	}
	/**
	 * 取多个用户信息
	 *
	 * @param string $nicks 淘宝用户名，以逗号分隔如"alipublic01,alipublic02"
	 * @param string $sip_format
	 * @return xml
	 */
	function getUsersInfo($nicks, $sip_format = 'xml'){

		$formVars['sip_timestamp'] = $this->SIP_TIMESTAMP;
		$formVars['sip_appkey'] = $this->APP_ID;
		$formVars['sip_apiname'] = "taobao.users.get";
		$formVars['v'] = $this->SIP_VER;
		$formVars['fields'] = "nick,sex,buyer_credit,seller_credit,location.city,location.state,location.country,created,last_visit";
		$formVars['nicks'] = $nicks;
		$formVars['sip_sessionid'] = $this->SIP_SESSIONID;
		$formVars['format'] = 'xml';
		ksort($formVars);

		$signStr = $this->CERT_CODE;
		foreach ($formVars as $key => $val) {
			$signStr.= $key;
			$signStr.= $val;
		}
		$sip_sign = strtoupper(md5($signStr));
		$formVars['sip_sign'] = $sip_sign;

		$this->proxy->submit($this->SIP_URL_PRE,$formVars);
		return ($this->proxy->results);

	}
	/**
	 * 取得店铺分类
	 *
	 * @param int $parent_cid 父ID，0为根分类
	 * @param string $sip_format
	 * @return xml
	 */
	function taobao_getShopCates($parent_cid = '0', $sip_format = 'xml'){

		$formVars['sip_timestamp'] = $this->SIP_TIMESTAMP;
		$formVars['sip_appkey'] = $this->APP_ID;
		$formVars['sip_apiname'] = 'taobao.shopcats.list.get';
		$formVars['v'] = $this->SIP_VER;
		$formVars['parent_cid'] = $parent_cid;
		$formVars['sip_sessionid'] = $this->SIP_SESSIONID;
		$formVars['format'] = $sip_format;
		ksort($formVars);
		//  print_r($formVars);
		//生成签名
		$signStr = $this->CERT_CODE;
		foreach ($formVars as $key => $val) {
			$signStr.= $key;
			$signStr.= $val;
		}
		$sip_sign = strtoupper(md5($signStr));
		$formVars['sip_sign'] = $sip_sign;
		//向AEP发出服务端请求
		$this->proxy->submit($this->SIP_URL_PRE,$formVars);

		return ($this->proxy->results);
	}
	/**
	 * 获得淘宝商品分类
	 *
	 * @param int $parent_cid 根分类id，1为根分类。$cids和$parent_cid至少传一个。
	 * @param string $cids 类目id里列表，传输该参数返回类目列表。 $cids和$parent_cid至少传一个。
	 * @param string $sip_format 返回信息类型，可以为"xml"和"json"
	 * @return xml
	 */
	function taobao_getProductCatesList($parent_cid = '1', $cids = '', $sip_format = 'xml'){

		$formVars['sip_timestamp'] = $this->SIP_TIMESTAMP;
		$formVars['sip_appkey'] = $this->APP_ID;
		$formVars['sip_apiname'] = 'taobao.itemcats.list.get';
		$formVars['v'] = $this->SIP_VER;
		if($parent_cid != ''){
			$formVars['parent_cid'] = $parent_cid;
		}
		if($cids != ''){
			$formVars['cids'] = $cids;
		}
		$formVars['sip_sessionid'] = $this->SIP_SESSIONID;
		$formVars['format'] = $sip_format;
		ksort($formVars);
		//  print_r($formVars);
		//生成签名
		$signStr = $this->CERT_CODE;
		foreach ($formVars as $key => $val) {
			$signStr.= $key;
			$signStr.= $val;
		}
		$sip_sign = strtoupper(md5($signStr));
		$formVars['sip_sign'] = $sip_sign;
		//向AEP发出服务端请求
		$this->proxy->submit($this->SIP_URL_PRE,$formVars);

		return ($this->proxy->results);
	}
	/**
	 * 获得淘宝后台商品分类
	 *
	 * @param int $parent_cid 根分类id，1为根分类。$cids和$parent_cid至少传一个。
	 * @param string $cids 类目id里列表，传输该参数返回类目列表。 $cids和$parent_cid至少传一个。
	 * @param string $sip_format 返回信息类型，可以为"xml"和"json"
	 * @return xml
	 */
	function taobao_getProductCates($parent_cid = '0', $cids = '', $sip_format = 'xml'){

		$formVars['sip_timestamp'] = $this->SIP_TIMESTAMP;
		$formVars['sip_appkey'] = $this->APP_ID;
		$formVars['sip_apiname'] = 'taobao.itemcats.get';
		$formVars['v'] = $this->SIP_VER;
		if($parent_cid != ''){
			$formVars['parent_cid'] = $parent_cid;
		}
		if($cids != ''){
			$formVars['cids'] = $cids;
		}
		$formVars['sip_sessionid'] = $this->SIP_SESSIONID;
		$formVars['format'] = $sip_format;
		ksort($formVars);
		//  print_r($formVars);
		//生成签名
		$signStr = $this->CERT_CODE;
		foreach ($formVars as $key => $val) {
			$signStr.= $key;
			$signStr.= $val;
		}
		$sip_sign = strtoupper(md5($signStr));
		$formVars['sip_sign'] = $sip_sign;
		//向AEP发出服务端请求
		$this->proxy->submit($this->SIP_URL_PRE,$formVars);

		return ($this->proxy->results);
	}
	/**
	 * 获得淘宝商品属性
	 *
	 * @param int $cid 分类ID
	 * @param string $sip_format 返回信息类型，可以为"xml"和"json"
	 * @return unknown
	 */
	function taobao_getItemPropsList($cid = '1', $sip_format = 'xml'){

		$formVars['sip_timestamp'] = $this->SIP_TIMESTAMP;
		$formVars['sip_appkey'] = $this->APP_ID;
		$formVars['sip_apiname'] = 'taobao.itemprops.list.get';
		$formVars['v'] = $this->SIP_VER;
		$formVars['cid'] = $cid;
		$formVars['sip_sessionid'] = $this->SIP_SESSIONID;
		$formVars['format'] = $sip_format;
		ksort($formVars);
		//  print_r($formVars);
		//生成签名
		$signStr = $this->CERT_CODE;
		foreach ($formVars as $key => $val) {
			$signStr.= $key;
			$signStr.= $val;
		}
		$sip_sign = strtoupper(md5($signStr));
		$formVars['sip_sign'] = $sip_sign;
		//向AEP发出服务端请求
		$this->proxy->submit($this->SIP_URL_PRE,$formVars);

		return ($this->proxy->results);
	}
	/**
	 * 获得淘宝用户仓库里的商品
	 * @需要用户绑定
	 * @param 查询字段 $q
	 * @param string $sip_format 返回信息类型，可以为"xml"和"json"
	 * @return unknown
	 */
	function taobao_getItemsInstock($q = '', $sip_format = 'xml'){

		$formVars['sip_timestamp'] = $this->SIP_TIMESTAMP;
		$formVars['sip_appkey'] = $this->APP_ID;
		$formVars['sip_apiname'] = 'taobao.items.instock.get';
		$formVars['fields'] = 'iid,title,nick,price,pic_path,list_time,delist_time';
		$formVars['v'] = $this->SIP_VER;
		//$formVars['q'] = $q;
		$formVars['page_no'] = '1';
		$formVars['page_size'] = '10';
		$formVars['sip_sessionid'] = $this->SIP_SESSIONID;
		$formVars['format'] = $sip_format;
		ksort($formVars);

		//生成签名
		$signStr = $this->CERT_CODE;
		foreach ($formVars as $key => $val) {
			$signStr.= $key;
			$signStr.= $val;
		}
		$sip_sign = strtoupper(md5($signStr));
		$formVars['sip_sign'] = $sip_sign;
		//向AEP发出服务端请求
		$this->proxy->submit($this->SIP_URL_PRE,$formVars);

		return ($this->proxy->results);
	}
	/**
	 * 获取淘宝正在出售的商品
	 * @需要用户绑定
	 * @param string $q 查询字段
	 * @param string $sip_format 返回信息类型，可以为"xml"和"json"
	 * @return unknown
	 */
	function taobao_getItemsOnsale($q = '', $sip_format = 'xml'){

		$formVars['sip_timestamp'] = $this->SIP_TIMESTAMP;
		$formVars['sip_appkey'] = $this->APP_ID;
		$formVars['sip_apiname'] = 'taobao.items.onsale.get';
		$formVars['fields'] = 'iid,title,nick,price,list_time,delist_time';
		$formVars['v'] = $this->SIP_VER;
		//$formVars['q'] = $q;
		$formVars['page_no'] = '1';
		$formVars['page_size'] = '20';
		$formVars['sip_sessionid'] = $this->SIP_SESSIONID;
		$formVars['format'] = $sip_format;
		ksort($formVars);
		//生成签名
		$signStr = $this->CERT_CODE;
		foreach ($formVars as $key => $val) {
			$signStr.= $key;
			$signStr.= $val;
		}
		$sip_sign = strtoupper(md5($signStr));
		$formVars['sip_sign'] = $sip_sign;
		//向AEP发出服务端请求
		$this->proxy->submit($this->SIP_URL_PRE,$formVars);

		return ($this->proxy->results);
	}
}
?>